#include "TitlePart.h"
#include "base/core/gamemath.h"
#include "Scene/SceneMgr.h"
#include "base/core/log.h"
#include "base/core/random.h"
#include "Common/Utility/ServerTime.h"
#include "Common/Utility/AccountUtility.h"
#include "Character/Player.h"
#include "Common/TableData/TitleTitleCfg.h"
#include "CreatureMgr.h"
#include "Common/TableData/ItemItemCfg.h"
#include "Common/TableDataEx/TitleCfg.h"
#include "Common/AttrMgr/AttrMgr.h"
#include "Player.h"

#include "SkillPart.h"
#include "Common/PackageDefine.h"
#include "base/core/Profiler.h"
#include "Common/TableData/ConstantConstantCfg.h"
#include "Common//TableData/FunctionunlockFunctionUnlockCfg.h"
#include "Character/monster/Monster.h"
#include "Common/TableData/AchievementAchievementCfg.h"


TitlePart::TitlePart()
{
	m_mapMsgfunc[CLIENT_TO_LOGIC_REQUEST_TITLE_LIST] = &TitlePart::OnGetTitlelist;
	m_mapMsgfunc[CLIENT_TO_LOGIC_USE_TITLE]			 = &TitlePart::OnUseTitle;
	m_mapMsgfunc[CLIENT_TO_LOGIC_ADD_ATTR_TITLE]	 = &TitlePart::OnAddAttrTitle;
	m_mapMsgfunc[CLIENT_TO_LOGIC_USE_TITLE_CANCLE]   = &TitlePart::OnUseTitleCancle;
	m_mapMsgfunc[CLIENT_TO_LOGIC_ADD_ATTR_TITLE_CANCLE] = &TitlePart::OnAddAttrTitleCancle;
}

TitlePart::~TitlePart()
{

}


bool TitlePart::Init(Creature *pMaster, uint32_t partType, CharacterDBInfo* pCharacterDB)
{
	Part::Init(pMaster, partType);

	m_pTitlePool = new ObjectPool<stTitle>(100,false);
	m_pTitleMapPool = new ObjectPool<stEnterMapTitle>(5,false);
	m_mapTitle.clear();

	uint64_t nowSecTime = Time::Now().UnixSec();

	//先从配置表中把所有称号信息读出来
	const TitleTitleCfgMap * ptitleMap = g_GetTitleTitleCfgTable()->GetTitleTitleCfgMap();
	if (ptitleMap)
	{
		TitleTitleCfgMap::const_iterator iter = ptitleMap->begin();
		for (; iter != ptitleMap->end();++iter)
		{
			stTitle *  pTitleInfo = nullptr;
			if (iter->second.subType == enTitleFinishType_Explore_Map)
			{
				pTitleInfo = m_pTitleMapPool->MallocObj();
			}
			else
			{
				pTitleInfo = m_pTitlePool->MallocObj();
			}
			
			pTitleInfo->_ID = iter->first;
			pTitleInfo->_StartCanUseTime = 0;

			if (iter->second.subType == enTitleFinishType_Collect || iter->second.subType == enTitleFinishType_Forge)
			{
				pTitleInfo->_currentParam = 1;
			}
			else
				pTitleInfo->_currentParam = 0;

			if (iter->second.subType == enTitleFinishType_ChangeAttr)
			{

					VEC_INT32* pResult = g_GetTitleCfgMgr()->GetTitleParamByTitleID(iter->first);

					if (!pResult ||  pResult->size() < 2)
					{
						m_pTitlePool->FreeTrack(pTitleInfo);
						continue;
					}

					pTitleInfo->_key = g_GetAttrMgr()->GetPAttr((*pResult)[0]);

					int32_t nValue = m_pMaster->GetPAttrValue(g_GetAttrMgr()->GetPAttr((*pResult)[0]));
					if (g_GetAttrMgr()->GetPAttr((*pResult)[0]) == P_GOLD) //金币直接显示金币数量
					{
						nValue = nValue / 10000;
					}
					if (nValue >= (*pResult)[1])
					{
						pTitleInfo->_StartCanUseTime = nowSecTime;
						pTitleInfo->_currentParam = (*pResult)[1];
						//启定时器判断称号有效期
						if (iter->second.lifeTime > 0)
						{
							g_GetTimerAxis()->SetTimer(pTitleInfo->_ID, iter->second.lifeTime * 60 * 60 * 1000, this, 1);
						}
					}
					else
					{
						pTitleInfo->_currentParam = nValue;
					}
				}

			//用这个，做下属性改变称号优化
			m_mapAttrTitle[pTitleInfo->_key].push_back(pTitleInfo->_ID);

			m_mapTitle.insert(std::pair<uint32_t, stTitle*>(iter->first, pTitleInfo));

			m_mapTypeTitle[iter->second.subType].push_back(pTitleInfo->_ID);

		}
	}

	//1,初始化称号
	//2,把用户使用的称号属性给加上
	//3,去掉正在使用的过期的称号
	if (pCharacterDB->has_titledata())
	{
		for (int i = 0; i < pCharacterDB->titledata().titeldata_size(); ++i)
		{
			const CharacterTitleInfo & stCharacterTitle = pCharacterDB->titledata().titeldata(i);
			
			mapTitle::iterator iterTitle = m_mapTitle.find(stCharacterTitle.id());
			if (iterTitle == m_mapTitle.end())
			{
				DeleteTitle(iterTitle->second);
				continue;
			}

			iterTitle->second->Init((CharacterTitleInfo*)(&stCharacterTitle));

			const TitleTitleCfgInfo * pConfig = g_GetTitleTitleCfgTable()->GetTitleTitleCfgInfo(stCharacterTitle.id());
			//判断还是不是在有效期内
			int32_t leftTime = Time::Now().UnixSec() - m_mapTitle[stCharacterTitle.id()]->_StartCanUseTime;
			if (pConfig && m_mapTitle[stCharacterTitle.id()]->_StartCanUseTime > 0)
			{
				if (pConfig->lifeTime>0 && leftTime >= pConfig->lifeTime * 60 * 60)
				{
					iterTitle->second->_currentParam = 0;
					iterTitle->second->_StartCanUseTime = 0;
					//如果是已加称号，则作相应处理
					if ((int32_t)m_pMaster->GetPAttrValue(P_USE_TITLE) == (int32_t)stCharacterTitle.id())
					{
						m_pMaster->SetAttrPA(P_USE_TITLE, 0);
					}
					if ((int32_t)m_pMaster->GetPAttrValue(P_ADDATTR_TITLE) == (int32_t)stCharacterTitle.id())
					{
						AddTitleAttr(); //先把属性清空
						m_pMaster->SetAttrPA(P_ADDATTR_TITLE, 0);
					}
				}
				else
				{
					if (pConfig && (pConfig->lifeTime == 0 || leftTime < pConfig->lifeTime * 60 * 60))
					{
						//如果是已加称号，则作相应处理
						if ((int32_t)m_pMaster->GetPAttrValue(P_ADDATTR_TITLE) == (int32_t)stCharacterTitle.id())
						{
							AddTitleAttr(stCharacterTitle.id());
						}
					}

					if (leftTime < pConfig->lifeTime * 60 * 60)
					{
						//加有效时间定时器
						g_GetTimerAxis()->SetTimer(stCharacterTitle.id(), pConfig->lifeTime * 60 * 60 * 1000 - leftTime * 1000, this, 1);
						//g_GetTimerAxis()->SetTimer(stCharacterTitle.id(), 60*1000, this, 1);   //test
					}
				}

			}
		}
	}
	
	//初始化解锁等级
	const FunctionunlockFunctionUnlockCfgInfo * pConfigOpenLevel = g_GetFunctionunlockFunctionUnlockCfgTable()->GetFunctionunlockFunctionUnlockCfgInfo(FunctionUnlock_ID_TYPE_TITLE);
	if (pConfigOpenLevel)
	{
		m_nOpenLevel = pConfigOpenLevel->unlockLevel;
	}
	else
		m_nOpenLevel = 1;
	


	//注册事件
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_PET_FIGHT_POWER_CHANGE, m_pMaster->GetCid(), CREATURE_PET, "titlePart");
	//g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_CREATURE_DEAD, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_CREATURE_DEAD, 0, CREATURE_PLAYER, "titlePart");

	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_CREATURE_DEAD, 0, CREATURE_MONSTER, "titlePart");

	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_ITEM_CHANGE, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_FINISH_ACHIVEMENT, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_ARENA_LEVEL_UP, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_DEPUTY_COLLECT_LEVEL_UP, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_DEPUTY_FORGE_LEVEL_UP, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_DEPUTY_FORGE_EQUIP, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	//g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_EQUP_STREN, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_EQUIP_REFINE, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_GOD_LEVEL, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");

	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_EQUIP_REFINE_FAIL, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_FINISH_TASK, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_TREASURE_DRUG, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_DEPUTY_MAKE_OPERATE, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");

	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_CREATURE_CHANGE_SCENE, m_pMaster->GetCid(), 0, "titlePart");

	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_ADD_ACHIVEMENT_PROGRESS, m_pMaster->GetCid(), CREATURE_PLAYER, "titlePart");
	

	

	return true;
}

bool TitlePart::UnInit()
{

	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_PET_FIGHT_POWER_CHANGE, m_pMaster->GetCid(), CREATURE_PET);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_CREATURE_DEAD, 0, CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_CREATURE_DEAD, 0, CREATURE_MONSTER);

	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_ITEM_CHANGE, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_FINISH_ACHIVEMENT, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_ARENA_LEVEL_UP, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_DEPUTY_COLLECT_LEVEL_UP, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_DEPUTY_FORGE_LEVEL_UP, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_DEPUTY_FORGE_EQUIP, m_pMaster->GetCid(), CREATURE_PLAYER);
	//g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_EQUP_STREN, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_EQUIP_REFINE, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_GOD_LEVEL, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_EQUIP_REFINE_FAIL, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_FINISH_TASK, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_TREASURE_DRUG, m_pMaster->GetCid(), CREATURE_PLAYER);
	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_DEPUTY_MAKE_OPERATE, m_pMaster->GetCid(), CREATURE_PLAYER);

	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_ADD_ACHIVEMENT_PROGRESS, m_pMaster->GetCid(), CREATURE_PLAYER);

	g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_CREATURE_CHANGE_SCENE, m_pMaster->GetCid(), 0);

	mapTitle::iterator iter = m_mapTitle.begin();
	for (; iter != m_mapTitle.end();++iter)
	{
		DeleteTitle(iter->second);
	}

	m_mapTitle.clear();

	MMO_DELETE(m_pTitlePool);
	MMO_DELETE(m_pTitleMapPool);
	return true;
}

void TitlePart::OnTimer(uint32_t nTimerId)
{
	mapTitle::iterator iter = m_mapTitle.find(nTimerId);  //这个就是称号ID
	if (iter!=m_mapTitle.end())  //称号时间到，设置失效
	{
		iter->second->_currentParam = 0;
		iter->second->_StartCanUseTime = 0;
		//如果是已加属性的，则取消添加
		if (iter->second->_ID ==  (uint32_t)m_pMaster->GetPAttrValue(P_ADDATTR_TITLE))
		{
			m_pMaster->SetAttrPA(P_ADDATTR_TITLE, 0,true);
			AddTitleAttr();
			//是否需要通知客户端
			m_pMaster->SynAttrToClient();
		}
		else if (iter->second->_ID == (uint32_t)m_pMaster->GetPAttrValue(P_USE_TITLE))
		{
			m_pMaster->SetAttrPA(P_USE_TITLE, 0, true);

		}

		TitleDisable msgRsp;
		msgRsp.set_titleid(iter->second->_ID);
		m_pMaster->SendDataToClient(EMODULE_ID_TITLE, LOGIC_TO_CLIENT_TITLE_DISABLE, &msgRsp);
	}
}

void TitlePart::OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{

	//if (m_pMaster->GetPAttrValue(P_LEVEL)<m_nOpenLevel)
	//{
	//	return;
	//}

	switch (nEventID)
	{
	//case EVENT_ATTR_CHANGE:
	//{
	//	BEGIN_PROFILE("TitlePart::OnExecute");
	//	
	//	END_PROFILE();
	//}
	//break;
	case EVENT_PET_FIGHT_POWER_CHANGE:
	{
		PetFightPowerEvent * pEvent = static_cast<PetFightPowerEvent *>(pContext);
		if (pEvent)
		{
			OnTitleParamChange(enTitleFinishType_PetFightPower, C_FIGHT_POWER,pEvent->powerValue,0);
		}
	}
	break;
	case EVENT_CREATURE_DEAD:
	{
		DieEvent *pDieEvent = static_cast<DieEvent *>(pContext);
		if (pDieEvent)
		{
			Scene * pSene = g_GetSceneMgr()->GetScene(pDieEvent->sceneId);
			if (pSene)
			{
				Player * pPlayer=pSene->GetPlayer(pDieEvent->nCid);
				if (pPlayer)
				{
					if (pPlayer->GetCid() == m_pMaster->GetCid()) //我被杀的
					{
						OnTitleParamChange(enTitleFinishType_DeadNum, 0, 0, 1);
					}
					else if (pDieEvent->killerCid == m_pMaster->GetCid()) //我杀的人
					{
						OnTitleParamChange(enTitleFinishType_KillPlayer, 0, 0, 1);

						//只记录击杀敌对阵营的人数
						if (m_pMaster->GetPAttrValue(P_CAMP_ID)!=pPlayer->GetCampId())
						{
							OnTitleParamChange(enTitleFinishType_KillCampPlayer, 0, 0, 1);
						}
					}
				}
				else
				{
					Monster * pMonster = pSene->GetMonster(pDieEvent->nCid);
					if (pMonster)
					{
						if (pDieEvent->killerCid == m_pMaster->GetCid())
						{
							OnTitleParamChange(enTitleFinishType_KillMonster, pMonster->GetConfigId(), 0, 1);
						}
					}
				}
			}
		}
	}
	break;
	case EVENT_ITEM_CHANGE:
	{
		ItemChangeEvent * pEvent = static_cast<ItemChangeEvent *>(pContext);
		if (pEvent)
		{
			if (pEvent->changeType == S_Sale || pEvent->changeType == S_ACTUINEER)
			{
				break;
			}
			if (pEvent->itemNum>0)
			{
				OnTitleParamChange(enTitleFinishType_GetGem, pEvent->itemId, 0, pEvent->itemNum);
			}
			else
			{
				OnTitleParamChange(enTitleFinishType_UseItem, pEvent->itemId, 0, -1*pEvent->itemNum);
			}
		}
	}
	break;
	case EVENT_FINISH_ACHIVEMENT:
	{
		//AchevimentFinishEvent * pEvent = static_cast<AchevimentFinishEvent *>(pContext);
		//if (pEvent)
		//{
		//	//进度给最大值，超过最大进度值
		//	OnTitleParamChange(enTitleFinishType_Achieve, pEvent->achevimentID, g_MaxAchivementValu);
		//}
	}
	break;
	case EVENT_ADD_ACHIVEMENT_PROGRESS:
	{
		AchevimentProgressAddEvent * pEvent = static_cast<AchevimentProgressAddEvent *>(pContext);
		if (pEvent)
		{
			OnTitleParamChange(enTitleFinishType_Achieve, pEvent->achevimentID, pEvent->curProgress, 0);
		}
	}
	break;
	case EVENT_ARENA_LEVEL_UP:
	{
		ArenaLevelUpEvent * pEvent = static_cast<ArenaLevelUpEvent *>(pContext);
		if (pEvent)
		{
			OnTitleParamChange(enTitleFinishType_ArenaLevel, 0,pEvent->level,0);
		}
	}
	break;
	case EVENT_DEPUTY_COLLECT_LEVEL_UP:
	{
		CollectLevelUpEvent * pEvent = static_cast<CollectLevelUpEvent *>(pContext);
		if (pEvent)
		{
			OnTitleParamChange(enTitleFinishType_Collect, pEvent->type, pEvent->level, 0);
			OnTitleParamChange(enTitleFinishType_AnyCollectByWork_Skill, 0, pEvent->level, 0);
		}
	}
	break;
	case EVENT_DEPUTY_FORGE_LEVEL_UP:
	{
		ForgeLevelUpEvent * pEvent = static_cast<ForgeLevelUpEvent *>(pContext);
		if (pEvent)
		{
			OnTitleParamChange(enTitleFinishType_Forge, pEvent->type, pEvent->level, 0);
			OnTitleParamChange(enTitleFinishType_AnyMakeByWork_Skill, 0, pEvent->level, 0);
		}
	}
	break;
	case EVENT_DEPUTY_FORGE_EQUIP:
	{
		ForgeEquipEvent* pEvent = static_cast<ForgeEquipEvent *>(pContext);
		if (pEvent)
		{
			OnTitleParamChange(enTitleFinishType_GetEquip, pEvent->quality,0,1);
		}
	}
	break;
	case EVENT_ACTIVIYT_PASS:
	{
		ActivityPassEvent * pEvent = static_cast<ActivityPassEvent *>(pContext);
		if (pEvent)
		{

			OnTitleParamChange(enTitleFinishType_FinishActivity, pEvent->activityId, 0, 1);
		}
	}
	break;
	case EVENT_GOD_LEVEL:
	{
		GodLevelEvent * pEvent = static_cast<GodLevelEvent*>(pContext);
		if (pEvent)
		{

			OnTitleParamChange(enTitleFinishType_GodLevel, pEvent->godID, pEvent->level,0);
		}
	}
		break;
	case EVENT_EQUIP_REFINE:
	{
		EquipRefineEvent * pEvent = static_cast<EquipRefineEvent *>(pContext);
		if (pEvent)
		{
			OnTitleParamChange(enTitleFinishType_FinishRefineEquip, pEvent->slot, pEvent->refinLevel, 0);
		}
	}
	break;
	case EVENT_EQUIP_REFINE_FAIL:
	{
		OnTitleParamChange(enTitleFinishType_Refine_Fail, 0, 0, 0);
	}
	break;
	case EVENT_FINISH_TASK:
	{
		FinishTaskEvent * pEvent = static_cast<FinishTaskEvent *>(pContext);
		if (pEvent)
		{
			OnTitleParamChange(enTitleFinishType_Task, pEvent->taskId, 0, 0);
		}
	}
	break;
	case EVENT_TREASURE_DRUG:
	{
		OnTitleParamChange(enTitleFinishType_Trigger_Snare, 0, 0, 0);
	}
	break;
	case EVENT_DEPUTY_MAKE_OPERATE:
	{
		MakeOperateEvent * pEvent = static_cast<MakeOperateEvent *>(pContext);
		if (pEvent)
		{
			if (pEvent->type == EMakeDeputyType_Cook)
			{
				OnTitleParamChange(enTitleFinishType_Cooking, pEvent->itemID, 0, pEvent->itemNum);
			}
			else if (pEvent->type == EMakeDeputyType_Medicine)
			{
				OnTitleParamChange(enTitleFinishType_Making_Drug, pEvent->itemID, 0, pEvent->itemNum);
			}
		}
	}
	break;
	case EVENT_CREATURE_CHANGE_SCENE:
	{
		EnterSceneEvent *pEvent = (EnterSceneEvent*)pContext;
		if (pEvent)
		{
			OnTitleParamChange(enTitleFinishType_Explore_Map, pEvent->mapId, 0, 1);
		}
	}
	break;
	default:
		break;
	}
}

//称号参数改变
void TitlePart::OnTitleParamChange(enTitleFinishType type, int32_t param1, uint32_t Allparam, uint32_t changeParam)
{
	mapTypeTitle::iterator iter = m_mapTypeTitle.find(type);
	if (iter!=m_mapTypeTitle.end())
	{
		for (size_t i = 0; i < iter->second.size();++i)
		{
			const TitleTitleCfgInfo * pConfig = g_GetTitleTitleCfgTable()->GetTitleTitleCfgInfo(iter->second[i]);
			if (!pConfig)
			{
				continue;
			}

			mapTitle::iterator iterTitle= m_mapTitle.find(iter->second[i]);
			if (iterTitle==m_mapTitle.end())
			{
				continue;

			}

			if (iterTitle->second->_key>0)  //可以确定判断条件的，就在这里先判断，不能的，只能放后面具体类型中判断了
			{
				if (iterTitle->second->_key == param1)
				{

					if (iterTitle->second->_StartCanUseTime > 0 && !pConfig->addTime)  //已经完成了，就不用处理了
					{
						continue;
					}
					JudgeTitle(pConfig, param1, *iterTitle->second, Allparam, changeParam);
					return;
				}
			}
			else
			{

				if (iterTitle->second->_StartCanUseTime > 0 && !pConfig->addTime)  //已经完成了，就不用处理了
				{
					continue;
				}
				JudgeTitle(pConfig, param1, *iterTitle->second, Allparam, changeParam);
			}
		}
	}
}

//判断该称号是否已完成
bool TitlePart::JudgeTitle(const TitleTitleCfgInfo * pConfig, int32_t param1, stTitle & stTile, uint32_t Allparam, uint32_t changeParam)
{
	VEC_INT32 *pResult =g_GetTitleCfgMgr()->GetTitleParamByTitleID(pConfig->id);

	if (!pResult)
	{
		return false;
	}

	switch (pConfig->subType)
	{
	case enTitleFinishType_ChangeAttr:
	{
		if (pResult->size() < 2)
		{
			return false;
		}
		
		if (g_GetAttrMgr()->GetPAttr((*pResult)[0]) != (uint32_t)param1)
		{
			return false;
		}

		if (param1 == P_GOLD) //金币只显示金
		{
			stTile._currentParam = m_pMaster->GetPAttrValue(param1) / 10000;
		}
		else
		{
			stTile._currentParam = m_pMaster->GetPAttrValue(param1);
		}
		

		return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], 0, 0);
		
	}
	break;
	case enTitleFinishType_PetFightPower:
	{	
		if (pResult->size() < 1)
		{
			return false;
		}

		return JudgeFinish(pConfig, param1, stTile, (*pResult)[0], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_UseItem:
	{
		if (pResult->size() < 2)
		{
			return false;
		}
		if ((*pResult)[0] != param1)
		{
			return false;
		}

		return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_GetGem:
	{
		const ItemItemCfgInfo *pItemInfo = g_GetItemItemCfgTable()->GetItemItemCfgInfo(param1);
		if (pItemInfo && pItemInfo->subType >= (int32_t)EItemSubType::EItemSubType_stone_begin && pItemInfo->subType <= (int32_t)EItemSubType::EItemSubType_stone_end) //宝石类
		{
			if (pResult->size() < 2)
			{
				return false;
			}


			if (pItemInfo->itemLevel < (*pResult)[0])
			{
				return false;
			}

			return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
		}
	}
	break;
	case enTitleFinishType_Achieve:
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		if (param1 == (*pResult)[0])
		{
			const AchievementAchievementCfgInfo * pCfg=g_GetAchievementAchievementCfgTable()->GetAchievementAchievementCfgInfo(param1);
			if (pCfg)
			{
				if (Allparam > 0)
				{
					stTile._currentParam = Allparam;
				}
				else if (changeParam > 0)
				{
					stTile._currentParam += changeParam;
				}

				if (stTile._currentParam >= (uint32_t)pCfg->achievementObjectValue)
				{

					stTile._StartCanUseTime = Time::Now().UnixSec();
					stTile._currentParam = (uint32_t)pCfg->achievementObjectValue;
					//启定时器判断称号有效期
					if (pConfig->lifeTime > 0)
					{
						g_GetTimerAxis()->SetTimer(stTile._ID, pConfig->lifeTime * 60 * 60 * 1000, this, 1);
					}
					SendEnableTitle(stTile._ID);
					return true;
				}
			}
		}
	}
	break;
	case enTitleFinishType_Add_Achieve_Progress:   //这个只显示进度
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		if (Allparam > 0)
		{
			stTile._currentParam = Allparam;
		}
		else if (changeParam > 0)
		{
			stTile._currentParam += changeParam;
		}
	}
	break;
	case enTitleFinishType_AtttackValue:
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		if ((int32_t)m_pMaster->GetAttr(C_PROF) == ECHARACTER_PROF_MAGICIAN)
		{

			stTile._currentParam = m_pMaster->GetAttr(C_MAT);

		}
		else
		{
			stTile._currentParam = m_pMaster->GetAttr(C_AP);
		}

		if ((int32_t)stTile._currentParam >= (*pResult)[0])
		{
			stTile._StartCanUseTime = Time::Now().UnixSec();
			stTile._currentParam = (*pResult)[0];
			//启定时器判断称号有效期
			if (pConfig->lifeTime > 0)
			{
				g_GetTimerAxis()->SetTimer(stTile._ID, pConfig->lifeTime * 60 * 60 * 1000, this, 1);
			}
			SendEnableTitle(stTile._ID);
			return true;
		}
	}
	break;
	case enTitleFinishType_KillPlayer:
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		return JudgeFinish(pConfig, param1, stTile, (*pResult)[0], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_DeadNum:
	{
		if (pResult->size() < 1)
		{
			return false;
		}
		return JudgeFinish(pConfig, param1, stTile, (*pResult)[0], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_ArenaLevel:
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		if ((int32_t)Allparam <= (*pResult)[0])
		{
			stTile._StartCanUseTime = Time::Now().UnixSec();
			stTile._currentParam = Allparam;
			//启定时器判断称号有效期
			if (pConfig->lifeTime > 0)
			{
				g_GetTimerAxis()->SetTimer(stTile._ID, pConfig->lifeTime * 60 * 60 * 1000, this, 1);
			}
			SendEnableTitle(stTile._ID);
			return true;
		}

		//return JudgeFinish(pConfig, param1, stTile, (*pResult)[0], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_KillCampPlayer:
	{
		if (pResult->size() < 1)
		{
			return false;
		
		}
	
		return JudgeFinish(pConfig, param1, stTile, (*pResult)[0], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_Collect:
	{
		if (pResult->size() < 2)
		{
			return false;
		}
		if (param1 != (*pResult)[0])
		{
			return false;
		}

		return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_Forge:
	{
			if (pResult->size() < 2)
			{
				return false;
			}
			if (param1 != (*pResult)[0])
			{
				return false;
			}

			return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_GetEquip:
	{
		if (pResult->size() < 2 || param1 != (*pResult)[0])
		{
			return false;
		}
		
		return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_FinishActivity:
	{
		if (pResult->size() < 2)
		{
			return false;
		}

		if (param1 != (*pResult)[0])
		{
			return false;
		}

		return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_GodLevel:
	{
			if (pResult->size() < 2)
			{
				return false;
			}
			//const EquipEquipCfgInfo *pEquip = g_GetEquipEquipCfgTable()->GetEquipEquipCfgInfo(param1);
			//if (!pEquip)
			//{
			//	return false;

			//}
											 
			if (param1 == (*pResult)[0])
			{
				return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
			}
											   
	}
	break;
	case enTitleFinishType_FinishRefineEquip:
	{
		if (pResult->size() < 2)
		{
			return false;
		}
		//const EquipEquipCfgInfo *pEquip = g_GetEquipEquipCfgTable()->GetEquipEquipCfgInfo(param1);
		//if (!pEquip)
		//{
		//	return false;

		//}

		if ((*pResult)[0] == 0 || param1 == (*pResult)[0])
		{
			return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
		}
	}
	break;
	case enTitleFinishType_Refine_Fail:
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		int32_t rand = Random(0, 10000);
		if (rand <(*pResult)[0]) //完成该称号
		{
			stTile._StartCanUseTime = Time::Now().UnixSec();
			stTile._currentParam = Allparam;
			//启定时器判断称号有效期
			if (pConfig->lifeTime > 0)
			{
				g_GetTimerAxis()->SetTimer(stTile._ID, pConfig->lifeTime * 60 * 60 * 1000, this, 1);
			}
			SendEnableTitle(stTile._ID);

			//取消订阅的事件
			g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_EQUIP_REFINE_FAIL, m_pMaster->GetCid(), CREATURE_PLAYER);
			return true;
		}
	}
	break;
	case enTitleFinishType_Task:
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		if (param1 ==(*pResult)[0]) //完成该称号
		{
			stTile._StartCanUseTime = Time::Now().UnixSec();
			stTile._currentParam = Allparam;
			//启定时器判断称号有效期
			if (pConfig->lifeTime > 0)
			{
				g_GetTimerAxis()->SetTimer(stTile._ID, pConfig->lifeTime * 60 * 60 * 1000, this, 1);
			}
			SendEnableTitle(stTile._ID);

			//取消订阅的事件
			g_GetEvent()->UnSubscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_FINISH_TASK, m_pMaster->GetCid(), CREATURE_PLAYER);

			return true;
		}
	}
	break;
	case enTitleFinishType_Trigger_Snare:
	{
		if (pResult->size() < 1)
		{
			return false;
		}
		return JudgeFinish(pConfig, param1, stTile, (*pResult)[0], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_KillMonster:
	{
		if (pResult->size() < 2)
		{
			return false;
		}
		if (param1 == (*pResult)[0])
		{
			return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
		}
	}
	break;
	case enTitleFinishType_AnyCollectByWork_Skill:
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		return JudgeFinish(pConfig, param1, stTile, (*pResult)[0], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_AnyMakeByWork_Skill:
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		return JudgeFinish(pConfig, param1, stTile, (*pResult)[0], Allparam, changeParam);
	}
	break;
	case enTitleFinishType_Cooking:
	case enTitleFinishType_Making_Drug:
	{
		if (pResult->size() < 2)
		{
			return false;
		}

		const ItemItemCfgInfo *pItemInfo = g_GetItemItemCfgTable()->GetItemItemCfgInfo(param1);
		if (pItemInfo && pItemInfo->itemLevel > (*pResult)[0])
		{
			return JudgeFinish(pConfig, param1, stTile, (*pResult)[1], Allparam, changeParam);
		}
	}
	break;
	case enTitleFinishType_Explore_Map:
	{
		if (pResult->size() < 1)
		{
			return false;
		}

		stEnterMapTitle * pInfo = static_cast<stEnterMapTitle*>(&stTile);
		if (pInfo)
		{
			//先找是否是已走过的地图
			std::vector<uint32_t>::iterator iterMap=std::find(pInfo->vMap.begin(), pInfo->vMap.end(), param1);
			if (iterMap==pInfo->vMap.end())
			{
				pInfo->vMap.push_back(param1);
				return JudgeFinish(pConfig, param1, stTile, (*pResult)[0], Allparam, changeParam);
			}
		}
	}
	break;
	default:
		break;
	};

	return false;
}

//完成条件判断
bool TitlePart::JudgeFinish(const TitleTitleCfgInfo * pConfig, int32_t param1, stTitle & stTile, int32_t orderNum, uint32_t Allparam, uint32_t changeParam)
{
	if (Allparam > 0)
	{
		stTile._currentParam = Allparam;
	}
	else if (changeParam > 0)
	{
		stTile._currentParam += changeParam;
	}

	bool isFinish = false;

	if (param1 == P_OFFICIAL || param1 == P_KNIGHT || param1 == P_UNION_DUTY)
	{
		if (stTile._currentParam == (uint32_t)orderNum)
		{
			isFinish = true;
		}
	}
	else
	{
		if (stTile._currentParam >= (uint32_t)orderNum)
		{
			isFinish = true;
		}
	}

	if (isFinish)
	{
		stTile._StartCanUseTime = Time::Now().UnixSec();
		stTile._currentParam = orderNum;
		//启定时器判断称号有效期
		if (pConfig->lifeTime > 0)
		{
			g_GetTimerAxis()->SetTimer(stTile._ID, pConfig->lifeTime * 60 * 60 * 1000, this, 1);
		}
		SendEnableTitle(stTile._ID);
		return true;
	}

	return false;
}

bool TitlePart::OnMsgHandle(uint32_t cmd, const char* data, uint32_t len)
{
	mapMsgFunc::iterator iter = m_mapMsgfunc.find(cmd);
	if (iter == m_mapMsgfunc.end())
		LogErrFmtPrint("msg dont exit in titlepart, cmd:%d", cmd);
	else
		(this->*m_mapMsgfunc[cmd])(data, len);

	return true;
}

void TitlePart::Update(uint64_t tick)
{

}

bool TitlePart::SaveDB(CharacterDBInfo& characterDB)
{
	CharacterTitleSaveData * pData = characterDB.mutable_titledata();
	mapTitle::iterator iter = m_mapTitle.begin();
	for (; iter != m_mapTitle.end();++iter)
	{
		//if (iter->second._StartCanUseTime>0 || iter->second._currentParam>0)
		//{
		//	//需要保存
		//	CharacterTitleInfo* pInfo = pData->add_titeldata();
		//	pInfo->set_id(iter->second._ID);
		//	pInfo->set_param(iter->second._currentParam);
		//	pInfo->set_starttime(iter->second._StartCanUseTime);
		//}
		iter->second->SaveDB(pData);
	}
	return true;
}

bool TitlePart::SynOnLogin(PlayerClientInfo& playerInfo)
{
	return true;
}

//加称号属性,id大于0表示添加，等于0表示清空
bool TitlePart::AddTitleAttr(uint32_t titleID)
{
	Player * pPlayer = dynamic_cast<Player*>(m_pMaster);
	if (!pPlayer)
	{
		return false;
	}
	//加称号前，先把称号相关属性清空
	for (uint32_t i = g_GetAttrMgr()->GetTitlePNumStartIndex(); i < g_GetAttrMgr()->GetTitlePNumEndIndex(); ++i)
	{
		m_pMaster->SetAttrPA(i, 0);
	}

	//根据titleID加属性
	if (titleID>0)
	{
		const TitleTitleCfgInfo * pConfig = g_GetTitleTitleCfgTable()->GetTitleTitleCfgInfo(titleID);
		if (pConfig)
		{
			for (int j = 0; j < (int)pConfig->vecTitleTitleAttributeCfg.size(); ++j)
			{
				TitleTitleAttributeCfgInfo date = pConfig->vecTitleTitleAttributeCfg.at(j);
				pPlayer->AddAttrPACache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Title, date.type), date.value);
			}
		}
	}

	return true;
}

bool TitlePart::OnGetTitlelist(const char* data, uint32_t len)
{
	GetTitleListReq useReq;
	if (!useReq.ParseFromArray(data, len))
		return false;

	//下发列表
	TitleListRsp msgRsp;
	mapTitle::iterator iter = m_mapTitle.begin();
	for (; iter != m_mapTitle.end();++iter)
	{
		stTitleInfo * pInfo = msgRsp.add_titleinfo();
		pInfo->set_titleid(iter->second->_ID);
		pInfo->set_starttitletime(iter->second->_StartCanUseTime);
		pInfo->set_param(iter->second->_currentParam);
	}

	//下发
	m_pMaster->SendDataToClient(EMODULE_ID_TITLE, LOGIC_TO_CLIENT_TITLE_LIST, &msgRsp);
	return true;
}

bool TitlePart::OnUseTitle(const char* data, uint32_t len)
{
	UseTitleReq useReq;
	UseTitleRsp msgRsp;
	if (!useReq.ParseFromArray(data, len))
		return false;

	//先判断玩家状态
	Player * pPlayer = dynamic_cast<Player*>(m_pMaster);
	if (pPlayer)
	{
		if (pPlayer->InSonOfKillingStatus())
		{
			msgRsp.set_retcode(RET_TITLE_IN_BUFF);
			m_pMaster->SendDataToClient(EMODULE_ID_TITLE, LOGIC_TO_CLIENT_USE_TITLE_RESULT, &msgRsp);
			return true;
		}
	}
	//判断称号是否存在
	mapTitle::iterator iter = m_mapTitle.find(useReq.titleid());
	if (iter==m_mapTitle.end())
	{
		msgRsp.set_retcode(RET_TITLE_NOT_EXIST);
	}
	//判断是否完成称号
	else if (iter->second->_StartCanUseTime<=0)
	{
		msgRsp.set_retcode(RET_TITLE_NOT_FINISH);
	}
	else if (useReq.titleid()==(int32_t)m_pMaster->GetPAttrValue(P_USE_TITLE))
	{
		msgRsp.set_retcode(RET_TITLE_HAS_USE);
	}
	else
	{
		msgRsp.set_retcode(RET_SUCCESS);
		
		m_pMaster->SetAttrPA(P_USE_TITLE, useReq.titleid(),true);	
	}
	


	m_pMaster->SendDataToClient(EMODULE_ID_TITLE, LOGIC_TO_CLIENT_USE_TITLE_RESULT, &msgRsp);
	return true;
}

bool TitlePart::OnAddAttrTitle(const char* data, uint32_t len)
{
	AddAttrTitleReq useReq;
	ADDAttrTitleRsp msgRsp;
	if (!useReq.ParseFromArray(data, len))
		return false;
	//判断称号是否存在
	mapTitle::iterator iter = m_mapTitle.find(useReq.titleid());
	if (iter == m_mapTitle.end())
	{
		msgRsp.set_retcode(RET_TITLE_NOT_EXIST);
	}
	//判断是否完成称号
	else if (iter->second->_StartCanUseTime <= 0)
	{
		msgRsp.set_retcode(RET_TITLE_NOT_FINISH);
	}
	else if (useReq.titleid() == (int32_t)m_pMaster->GetPAttrValue(P_ADDATTR_TITLE))
	{
		msgRsp.set_retcode(RET_TITLE_HAS_ADD_ATTR);
	}
	else
	{
		msgRsp.set_retcode(RET_SUCCESS);
		AddTitleAttr();
		AddTitleAttr(useReq.titleid());
		Player * pPlayer = dynamic_cast<Player*>(m_pMaster);
		if (pPlayer)
		{
			pPlayer->SetAttrPACache(P_ADDATTR_TITLE, useReq.titleid());
			pPlayer->SynAttrToClient();
		}
	}
	m_pMaster->SendDataToClient(EMODULE_ID_TITLE, LOGIC_TO_CLIENT_ADD_ATTR_TITLE_RESULT, &msgRsp);
	return true;
}

bool TitlePart::OnUseTitleCancle(const char* data, uint32_t len)
{
	UseTitleCancleReq useReq;
	UseTitleCancleRsp msgRsp;
	if (!useReq.ParseFromArray(data, len))
		return false;
	//判断称号是否存在
	mapTitle::iterator iter = m_mapTitle.find(useReq.titleid());
	if (iter == m_mapTitle.end())
	{
		msgRsp.set_retcode(RET_TITLE_NOT_EXIST);
	}
	//判断是否完成称号
	else if (iter->second->_StartCanUseTime <= 0)
	{
		msgRsp.set_retcode(RET_TITLE_NOT_FINISH);
	}
	else if (useReq.titleid() != (int32_t)m_pMaster->GetPAttrValue(P_USE_TITLE))
	{
		msgRsp.set_retcode(RET_TITLE_NOT_USE);
	}
	else
	{
		msgRsp.set_retcode(RET_SUCCESS);
		m_pMaster->SetAttrPA(P_USE_TITLE, 0, true);
	}
	m_pMaster->SendDataToClient(EMODULE_ID_TITLE, LOGIC_TO_CLIENT_USE_TITLE_CANCLE_RESULT, &msgRsp);
	return true;
}

bool TitlePart::OnAddAttrTitleCancle(const char* data, uint32_t len)
{
	AddAttrTitleCancleReq useReq;
	AddAttrTitleCancleRsp msgRsp;
	if (!useReq.ParseFromArray(data, len))
		return false;
	//判断称号是否存在
	mapTitle::iterator iter = m_mapTitle.find(useReq.titleid());
	if (iter == m_mapTitle.end())
	{
		msgRsp.set_retcode(RET_TITLE_NOT_EXIST);
	}
	//判断是否完成称号
	else if (iter->second->_StartCanUseTime <= 0)
	{
		msgRsp.set_retcode(RET_TITLE_NOT_FINISH);
	}
	else if (useReq.titleid() != (int32_t)m_pMaster->GetPAttrValue(P_ADDATTR_TITLE))
	{
		msgRsp.set_retcode(RET_TITLE_NOT_ADD_ATTR);
	}
	else
	{
		msgRsp.set_retcode(RET_SUCCESS);
		AddTitleAttr();
		Player * pPlayer = dynamic_cast<Player*>(m_pMaster);
		if (pPlayer)
		{
			pPlayer->SetAttrPACache(P_ADDATTR_TITLE,0);
			pPlayer->SynAttrToClient();
		}
		
	}
	m_pMaster->SendDataToClient(EMODULE_ID_TITLE, LOGIC_TO_CLIENT_USE_TITLE_CANCLE_RESULT, &msgRsp);
	return true;
}

void TitlePart::SendEnableTitle(uint32_t TitleID)
{
	//下发列表
	ActiveTitleRsp msgRsp;
	mapTitle::iterator iter = m_mapTitle.find(TitleID);
	if (iter!=m_mapTitle.end())
	{
		stTitleInfo  *pInfo = msgRsp.mutable_stinfo();
		pInfo->set_titleid(iter->second->_ID);
		pInfo->set_starttitletime(iter->second->_StartCanUseTime);
		pInfo->set_param(iter->second->_currentParam);
		
	}

	//下发
	m_pMaster->SendDataToClient(EMODULE_ID_TITLE, LOGIC_TO_CLIENT_TITLE_ACTIVE, &msgRsp);


	AddTitleEvent titleEvent;
	titleEvent.titleID = TitleID;
	g_GetEvent()->FireExecute(EVENT_ADD_TITLE, m_pMaster->GetCid(), CREATURE_PLAYER, (AddTitleEvent*)&titleEvent, sizeof(AddTitleEvent));
}


//int64_t leftValidTime 称号剩余有效时间，单位秒
void TitlePart::FinishTitle(uint32_t titleID, int64_t leftValidTime)
{
	mapTitle::iterator iter = m_mapTitle.find(titleID);
	if (iter == m_mapTitle.end())
	{
		return;
	}
	iter->second->_StartCanUseTime = Time::Now().UnixSec();
	iter->second->_currentParam = 0;
	//启定时器判断称号有效期
	if (leftValidTime > 0)
	{
		g_GetTimerAxis()->SetTimer(titleID, leftValidTime * 1000, this, 1);
	}
	SendEnableTitle(titleID);
}

bool TitlePart::ResetOneAttrAllTitle(uint32_t PANum,uint32_t value)
{
	mapAttrTitle::iterator iter = m_mapAttrTitle.find(PANum);
	if (iter == m_mapAttrTitle.end())
	{
		return false;
	}

	uint32_t nCurrentUseTitle = m_pMaster->GetPAttrValue(P_USE_TITLE);
	uint32_t nCurrentAddTitle = m_pMaster->GetPAttrValue(P_ADDATTR_TITLE);

	//所有已完成的重置
	std::vector<uint32_t>& vTitle = iter->second;
	for (int i = 0; i < (int)vTitle.size(); ++i)
	{
		mapTitle::iterator iterNew = m_mapTitle.find(vTitle[i]);
		if (iterNew != m_mapTitle.end())
		{
			if (iterNew->second->_StartCanUseTime>0 )
			{
				if (value == 0 || value != iterNew->second->_currentParam)
				{

					iterNew->second->_StartCanUseTime = 0;
					iterNew->second->_currentParam = 0;

					//如果是正在使用，或是正在加属性，则要处理掉
					if (nCurrentUseTitle > 0 && nCurrentUseTitle == iterNew->first)
					{
						m_pMaster->SetAttrPA(P_USE_TITLE, 0, true);
					}

					if (nCurrentAddTitle > 0 && nCurrentAddTitle == iterNew->first)
					{
						AddTitleAttr();
						m_pMaster->SetAttrPA(P_ADDATTR_TITLE, 0, false);
						m_pMaster->SynAttrToClient();
					}

					return true;
				}
				else
				{
					return false;
				}
			}
		}
	}

	return true;
}

//重置公会和阵营相关称号
bool TitlePart::ResetClanTitle(uint32_t value)
{
	return ResetOneAttrAllTitle(P_UNION_DUTY, value);
}

bool TitlePart::ResetCampTitle(uint32_t value)
{
	ResetOneAttrAllTitle(P_OFFICIAL,0);
	ResetOneAttrAllTitle(P_KNIGHT,0);
	return true;
}

//处理属性相关称号
void TitlePart::DoAttrTitle(uint32_t PANum, int64_t nValue)
{

	if (m_pMaster->GetPAttrValue(P_LEVEL) < m_nOpenLevel)
	{
		return;
	}

	const TitleTitleCfgInfo * pConfig = NULL;
	if (PANum == P_AP || PANum == P_MP)
	{
		OnTitleParamChange(enTitleFinishType_AtttackValue, PANum, nValue);
	}
	else
	{

		//如果是公会 ，阵营，要作预处理
		//如果是清空公会，或是阵营，则去掉所有公会 ，阵营相关的属性 
		if ((PANum == P_UNION_ID || PANum == P_CAMP_ID) && m_pMaster->GetPAttrValue(PANum) <= 0)
		{
			if (PANum == P_UNION_ID)
			{
				ResetClanTitle(0);
			}
			else
			{
				ResetCampTitle(0);
			}

			return;
		}


		mapAttrTitle::iterator iter=m_mapAttrTitle.find(PANum);
		if (iter == m_mapAttrTitle.end())
		{
			return;
		}


		//公会职位有唯一性，如果有变化，则要把以前的清空，头上的，加了属性的全清空,再走添加流程
		if (PANum == P_UNION_DUTY)
		{
			if (!ResetClanTitle(nValue))
			{
				//已有相同的称号，不处理
				return;
			}
		}

		if (PANum == P_OFFICIAL || PANum == P_KNIGHT)
		{
			if (!ResetOneAttrAllTitle(PANum, nValue))
			{
				//已有相同的称号，不处理
				return;
			}
		}
		
		std::vector<uint32_t> vTitle = iter->second;
		std::vector<uint32_t>::iterator iterTitle = vTitle.begin();
		for (; iterTitle != vTitle.end(); ++iterTitle)
		{

			pConfig = g_GetTitleTitleCfgTable()->GetTitleTitleCfgInfo(*iterTitle);
			if (!pConfig || pConfig->subType != enTitleFinishType_ChangeAttr)
			{
				continue;
			}

			mapTitle::iterator iterNew = m_mapTitle.find(*iterTitle);
			if (iterNew!=m_mapTitle.end())
			{
				if (iterNew->second->_StartCanUseTime > 0 && !pConfig->addTime)  //已经完成了，就不用处理了
				{
					continue;
				}
			
				JudgeTitle(pConfig, PANum, *iterNew->second, nValue, 0);
			}
		}
	}
}


//添加排名活动称号
void TitlePart::AddRankingList(uint32_t type, uint32_t index)
{
	if (type == CenterToLogicRspRankingList::en_type_fight)
	{
		std::vector<uint32_t> v = m_mapTypeTitle[enTitleRankingList_Fight];
		for (int i = 0; i < (int)v.size();++i)
		{
			VEC_INT32* pResult = g_GetTitleCfgMgr()->GetTitleParamByTitleID(v[i]);
			if (pResult && (int)pResult->size() == 1 && pResult->at(0)==(int32_t)index)
			{
				FinishTitle(v[i], 0);
				break;
			}
		}
	}
	else if (type == CenterToLogicRspRankingList::en_type_level)
	{
		std::vector<uint32_t> v = m_mapTypeTitle[enTitleRankingList_Level];
		for (int i = 0; i < (int)v.size(); ++i)
		{
			VEC_INT32* pResult = g_GetTitleCfgMgr()->GetTitleParamByTitleID(v[i]);
			if (pResult && (int)pResult->size() == 1 && pResult->at(0) == (int32_t)index)
			{
				FinishTitle(v[i], 0);
				break;
			}
		}
	}
}

//删除一个称号
void TitlePart::DeleteTitle(stTitle*pTitle)
{
	if (!pTitle)
	{
		return;
	}
	const TitleTitleCfgInfo * pConfig = g_GetTitleTitleCfgTable()->GetTitleTitleCfgInfo(pTitle->_ID);
	if (!pConfig)
	{
		return;
	}
	if (pConfig->subType == enTitleFinishType_Explore_Map)
	{
		m_pTitleMapPool->FreeTrack(static_cast<stEnterMapTitle*>(pTitle));
	}
	else
	{
		m_pTitlePool->FreeTrack(pTitle);
	}
}